package com.zhj.interview;

import java.util.concurrent.Semaphore;

/**
 * @author zhj
 */
public class SemaphoreTest {

    public static void main(String[] args) {
        Semaphore semaphore = new Semaphore(3);
        for (int i = 0; i < 10; i++) {
            new Thread(() -> {
                try {
                    // 获得许可
                    semaphore.acquire();
                    System.out.println(Thread.currentThread().getName() + "运行开始");
                    Thread.sleep(100);
                    System.out.println(Thread.currentThread().getName() + "运行结束");
                    // 释放许可
                    semaphore.release();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }, "Thread " + i).start();
        }
    }
}
